package com.study.algorithm.base.common;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.RandomUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ListUtil
 *
 * @author wanghuilong
 * @version 1.0
 * @description list util
 * @date 2022/11/6 15:08
 */
public class ListUtil {


    public static List<Integer> getRandomList(Integer num) {
        List<Integer> list = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            list.add(RandomUtils.nextInt(1, num * 100));
        }
        return list;
    }

    public static void  swap(List<Integer> list,int i,int k) {
        if(i != k) {
            list.set(i, list.get(k) ^ list.get(i));
            list.set(k, list.get(k) ^ list.get(i));
            list.set(i, list.get(k) ^ list.get(i));
        }
    }

    public static void  swap(int[] list,int i,int k) {
        if(i != k) {
            list[i] = list[k] ^ list[i];
            list[k] = list[k] ^ list[i];
            list[i] = list[k] ^ list[i];
        }
    }

    public static int[] getArr(List<Integer> list) {
        if(CollectionUtils.isEmpty(list)) {
            return new int[0];
        }
        int[] arr = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

}
